# -*- coding: utf-8 -*-
# @Date   : 2020/1/17
# @File   : AC02_Score_Change_Value.py
# @Author : zhaochen


'''eof
name:企业未来12个月未结清到期贷款笔数_change_value
code:AC02_Score_Change_Value
tableName:
columnName:
groups:AC模块
dependencies:EG_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''


import sys
import json

reload(sys)
sys.setdefaultencoding('utf-8')
null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']


def isFloat(x):
    '''
    判断x为float类型且大于等于0
    '''
    try:
        if isinstance(float(x), float) and float(x) >= 0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False


def getRatingInfoData(code, interface=None, last=False):
    '''
    获取上次评级信息的指标值或接口数据，默认返回的格式是tuple(val, "value") 指标值和标记
    标记分为 'value':指标值 'data':数据
    :param code: 要获取的指标值的指标code
    :param interface: EC_DSJ_2D/IC_DSJ_2D/TX_CQ_DSJ
    :return:
    '''
    try:
        historyRatingInfo = json.loads(ratingRequest.get("historyRatingInfo"))
        if last:
            # 获取上次评级数据信息
            ratingInfo = historyRatingInfo.get("lastRatingInfo")
        else:
            # 获取本次评级数据
            ratingInfo = historyRatingInfo.get("ratingInfo")
        # 如果是取score 或者 ratingLevel 直接返回
        if code in [ "ratingLevel"]:
            score = ratingInfo.get(code)
            try:
                return score, "value"
            except:
                return None, None
        # 取数据类型进行判断
        dataType = ratingInfo.get("dataType")
        # 如果传回来是数据 则返回相应接口里面的数据data
        if dataType == "1":
            data = ratingInfo.get(interface).get("data")
            if isinstance(data, dict):
                return (data, "data")
            else:
                return None, None
        # 如果传回来是指标值，则通过code进行提取
        elif dataType == "2":
            val = ratingInfo.get(code)
            if val in null_type_list:
                return None, None
            else:
                try:
                    return float(val), "value"
                except:
                    return None, None
        else:
            return None, None
    except:
        return None, None


def AC02_Score_Change_Value():
    level1, _ = getRatingInfoData('ratingLevel', last=False)
    level2, _ = getRatingInfoData('ratingLevel', last=True)
    level1=level1.lower()
    level2=level2.lower()
    if level1 in null_type_list or level1 == u'缺失值' or \
                    level2 in null_type_list or level2 == u'缺失值':
        return u'缺失值'
    else:
        if level1 == 'a':
            if level2 in ['a','b']:
                return 2
            elif level2 in ['c', 'd', 'e', 'f']:
                return 1
            else:
                return u'缺失值'
        elif level1 == 'b':
            if level2 in ['a']:
                return 3
            elif level2 in ['b', 'c']:
                return 2
            elif level2 in ['d', 'e', 'f']:
                return 1
            else:
                return u'缺失值'
        elif level1 == 'c':
            if level2 in ['a']:
                return 4
            elif level2 in ['b']:
                return 3
            elif level2 in ['c']:
                return 2
            elif level2 in ['d', 'e', 'f']:
                return 1
            else:
                return u'缺失值'
        elif level1 == 'd':
            if level2 in ['a', 'b']:
                return 4
            elif level2 in ['c']:
                return 3
            elif level2 in ['d']:
                return 2
            elif level2 in ['e', 'f']:
                return 1
            else:
                return u'缺失值'
        elif level1 == 'e':
            if level2 in ['a', 'b']:
                return 4
            elif level2 in ['c', 'd']:
                return 3
            elif level2 in ['e']:
                return 2
            elif level2 in ['f']:
                return 1
            else:
                return u'缺失值'
        elif level1 == 'f':
            if level2 in ['a', 'b', 'c']:
                return 4
            elif level2 in ['d']:
                return 3
            elif level2 in ['e', 'f']:
                return 2
            else:
                return u'缺失值'
        else:
            return u'缺失值'


result = AC02_Score_Change_Value()
